package com.spikything.ui {
	
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.geom.*;
	import com.spikything.utils.Utils;
	import com.spikything.ui.BarberGfx;
	
	[Embed(source="../../../../BuildAssets/SWFs/assets.swf", symbol="Progress")]
	public class Progress extends MovieClip {
		
		private static const OUTPOS:Number = -16;
		private static const INPOS:Number = 0;
		private static const COEFF:Number = 0.8;
		
		public var statusTxt:TextField;
		public var numTxt:TextField;
		public var barMC:MovieClip;
		public var underlineMC:MovieClip;
		public var statusBgMC:MovieClip;
		public var numBgMC:MovieClip;
		
		public var barberOffset:int;
		public var barb:Sprite;
		public var bd:BitmapData;
		
		private const OMNIPRESENT_PADDING:Number = 24;
		private const BARBER_OVERLAP:Number = 2;
		
		public function Progress(colour:uint, status:String="") {
			setColour(colour);
			setStatus(status);
			
			barb = new Sprite();
			addChild(barb);
			swapChildren(barb,statusTxt);
			swapChildren(barb,statusBgMC);
			var barberGfx:MovieClip = new BarberGfx();
			bd = new BitmapData(barberGfx.width-BARBER_OVERLAP, barberGfx.height, true, 0);
			bd.draw(barberGfx);
			var m1:Matrix = new Matrix();
			m1.translate(-barberGfx.width+BARBER_OVERLAP, 0);
			bd.draw(barberGfx, m1);
			var m2:Matrix = new Matrix();
			m2.translate(barberGfx.width-BARBER_OVERLAP, 0);
			bd.draw(barberGfx, m2);
			
			y = OUTPOS;
			addEventListener(Event.ENTER_FRAME, oefIn);
		}
		
		public function comeondown():void {
			removeEventListener(Event.ENTER_FRAME, oefOut);
			addEventListener(Event.ENTER_FRAME, oefIn);
		}
		
		public function vamoose():void {
			removeEventListener(Event.ENTER_FRAME, oefIn);
			addEventListener(Event.ENTER_FRAME, oefOut);
		}
		
		public function oefIn(e:Event):void {
			y = (y*COEFF)+(INPOS*(1-COEFF));
			if (Math.abs(y-INPOS) < 1) {
				y = INPOS;
				removeEventListener(Event.ENTER_FRAME, oefIn);
			}
		}
		
		public function oefOut(e:Event):void {
			y = (y*COEFF)+(OUTPOS*(1-COEFF));
			if (Math.abs(y-OUTPOS) < 1) {
				y = OUTPOS;
				removeEventListener(Event.ENTER_FRAME, doBarber);
				removeEventListener(Event.ENTER_FRAME, oefOut);
			}
		}
		
		/** @param perc Between 0 and 1 */
		public function setProgress(perc:Number):void {
			removeEventListener(Event.ENTER_FRAME, doBarber);
			barb.graphics.clear();
			numTxt.autoSize = TextFieldAutoSize.LEFT;
			numTxt.text = new String(Math.floor(perc * 100));
			numTxt.visible = true;
			var widthRemain:Number = 0;
			if (stage!=null) widthRemain = this.stage.stageWidth - OMNIPRESENT_PADDING - statusBgMC.width;
			barMC.width = Math.floor(OMNIPRESENT_PADDING + statusBgMC.width + (widthRemain * perc));
			barMC.visible = true;
			underlineMC.width = barMC.width;
			numBgMC.x = barMC.width + 1 - numBgMC.width;
			numTxt.x = numBgMC.x + 1;
		}
		
		public function setBarber():void {
			setProgress(1); // Hmm, shame
			numTxt.visible = false;
			numBgMC.visible = false;
			barberOffset = 0;
			addEventListener(Event.ENTER_FRAME, doBarber);
			doBarber();
		}
		
		public function doBarber(e:Event=null):void {
			barberOffset++;
			barb.graphics.clear();
			var n:Matrix = new Matrix();
			n.translate(barberOffset,0);
			barb.graphics.beginBitmapFill(bd, n);
			barb.graphics.drawRect(0, 0, barMC.width, barMC.height);
		}
		
		public function setStatus(s:String):void {
			statusTxt.autoSize = TextFieldAutoSize.LEFT;
			statusTxt.text = s;
			if (s=="") {
				statusBgMC.width = 0;
			} else {
				statusBgMC.width = (statusTxt.x * 2) + statusTxt.width;
			}
		}
		
		public function setColour(c:uint):void {
			Utils.tint(barMC, c);
			numTxt.textColor = c;
			statusTxt.textColor = c;
		}
		
	}
	
}